import 'package:citiesocial/_include.dart';
import 'package:citiesocial/widgets/goods/goods_in_order_detail.dart';
import 'package:flutter/services.dart';

class OrderDetail extends BaseStatefulWidget {
  final String orderId;
  OrderDetail(this.orderId);
  @override
  State<StatefulWidget> createState() => _OrderDetailState();
}

class _OrderDetailState extends BaseState<OrderDetail> {
  _OrderDetailState();
  OrderDetailEntity _detail;

  void _loadDetail() {
    HttpManager.orderDetail(Configs.userId, Configs.userToken, widget.orderId)
        .then((detail) {
      if (detail != null && detail.order_sn != null) {
        _detail = detail;
        setState(() {});
      }
    });
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadDetail();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final widgets = List<Widget>();
    if (_detail != null) {
      widgets.addAll(List.generate(_detail.goods?.length ?? 0,
          (index) => GoodsInOrderDetail(_detail.goods[index])));

      widgets.add(Container(
        color: Colors.white,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.symmetric(vertical: 10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                Text(
                  '订单号: ${_detail.order_sn}',
                  style: TextStyle(fontSize: 14.0, color: Colors.black),
                ),
                GestureDetector(
                  child: Container(
                    child: Text('复制',
                        style: TextStyle(
                            fontSize: 14.0, color: Colors.blueAccent)),
                    padding: EdgeInsets.symmetric(horizontal: 10.0),
                  ),
                  onTap: () {
                    Toast.show(context, '复制成功');
                    Clipboard.setData(
                        ClipboardData(text: '${_detail.order_sn}'));
                  },
                )
              ],
            ),
            Text(
              '下单时间: ${_detail.add_time}',
              style: TextStyle(fontSize: 14.0, color: Colors.black),
            ),
          ],
        ),
      ));

      widgets.add(Container(
        color: Colors.white,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.only(bottom: 10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              '支付方式: ${_detail.pay_way == '1' ? '微信支付' : '支付宝支付'}',
              style: TextStyle(fontSize: 14.0, color: Colors.black),
            ),
            Text(
              '支付时间: ${_detail.pay_time}',
              style: TextStyle(fontSize: 14.0, color: Colors.black),
            )
          ],
        ),
      ));

      widgets.add(Container(
        color: Colors.white,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.only(bottom: 10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              '${_detail.name}  ${_detail.email}',
              style: TextStyle(fontSize: 14.0, color: Colors.black),
            ),
            Text(
              '收货地址: ${_detail.address}',
              style: TextStyle(fontSize: 14.0, color: Colors.black),
            ),
          ],
        ),
      ));
    }
    return Scaffold(
      appBar: AppBarUtils.normal(context, '订单详情'),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: DListView(
          onRefresh: () async => _loadDetail(),
          children: widgets,
        ),
      ),
    );
  }
}
